<template>
    <div>
        <div class="search-content">
            <div>
                <p @click="changSelected">{{searchClass}}<i class="iconfont icon-xiala icon" :class="isShowSelect?'open':'hide'"></i></p>
                <div class="select-box" v-show="isShowSelect" @click="changSearch">
                    <p>商家</p>
                    <p>商品</p>
                </div>
            </div>
            <input type="text" placeholder="搜索美食..." v-model="searchText" @keyup.enter="toSearch">
            <button class="claer-btn" @click="clearSearch"><i class="iconfont icon-guanbifuceng"></i></button>
            <!-- <mt-button class="search-btn" @click="toSearch">搜索</mt-button> -->
        </div>
        <template v-if="!isShow">
            <ul>
                <li v-for="(item,key) in 6" :key="key">
                    <img src="../images/shop_back.svg" alt="back">
                </li>
            </ul>
        </template>
        <template v-else>
            <div class="class-list">
                <mt-swipe :auto="0">
                    <mt-swipe-item class="calss-page" v-for="(i,k) of class_list" :key="k">
                        <router-link :to="{path:'/classIndex',query:{id:item.id}}"  class="class-item" v-for="(item,index) of i" :key="index" :id="item.id">
                            <img :src="$store.getters.base_url+item.pic" class="class-img">
                            <p class="class-text">{{item.name}}</p>
                        </router-link>
                    </mt-swipe-item>
                </mt-swipe>
            </div>
            <ul class="store-list">
                <mt-loadmore :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" ref="loadmore" :auto-fill="false">
                    <storeList v-for="(item,key) in storeList" :key="key" :item='item' :delivery="delivery"></storeList>
                </mt-loadmore>
            </ul>
        </template>
    </div>
</template>
<script>
import { getStoreList,searchShopList,classList } from '../api'
// import Star from '../components/star'
import storeList from '../components/storeList'
import wxapi from '../wxapi.js';
import {wxSign} from '../api'
export default {
    data() {
        return {
            isShow:false,
            storeList:[],
            delivery:0,
            searchText:"",
            class_list:[],
            isShowSelect:false,
            searchClass:'商品',
            page:1,
            limit:10,
            allLoaded:false,
            type:1
        }
    },
    created(){
        this.getStoreList()
        const data = {
            token:this.$store.getters.token,
            url:window.location.href.split('#')[0],
            id:this.$store.getters.userInfo.id
        }
      this.getClassList();
      wxSign(data).then(res=>{
        console.log("参数获取成功")
        this.$store.commit('ADD_SUBSCRIBE',res.data.subscribe)
        const option = {
          title:"大学食代",
          link:'http://dxsd.jlxmall.com/Wxlogin/login?state='+this.$store.getters.userInfo.code,
          imgUrl:"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1584447750982&di=e936686168a314407bf206217c0ba92d&imgtype=0&src=http%3A%2F%2Fwww.helloedu.com.cn%2Fupload%2F2014%2F03%2F21%2F14%2F201403211446096946.jpg",
          desc:"专注于大学生外卖的移动外卖平台"
        }
        wxapi.wxRegister(res.data,option);
      })
    },
    methods:{
        toSearch() {this.page = 1;this.storeList = [];this.type=2;this.allLoaded = false;this.search()},
        loadBottom() {
            this.page++;
            if(this.type == 1){
                this.getStoreList();
            }else{
                this.search();
            }
            },
        changSelected(){
            this.isShowSelect = !this.isShowSelect
        },
        changSearch($event){
            this.isShowSelect = !this.isShowSelect;
            this.searchClass = $event.target.innerText;
        },
        getStoreList(){
            const data = {school_id:this.$store.getters.userInfo.school_id,token:this.$store.getters.token,page:this.page,limit:this.limit}
            getStoreList(data).then(res=>{
                console.log(res.data.delivery)
                this.delivery = res.data.delivery
                this.$store.commit("ADD_DELIVERY",{delivery:res.data.delivery,limit:res.data.limit,percentage:res.data.percentage})
                if(this.storeList.length){
                    this.storeList = this.storeList.concat(res.data.basic);
                    this.$refs.loadmore.onBottomLoaded();
                }else{
                    this.storeList = res.data.basic;
                    this.isShow = !this.isShow;
                }
                if(this.storeList.length < this.limit){
                    this.allLoaded = true;
                }
            }).catch(err=>{
                console.log(err)
            })
        },
        search(){
            const data = {school_id:this.$store.getters.userInfo.school_id,token:this.$store.getters.token,shop_name:this.searchText,type:this.searchClass=='商家'?'1':'2',page:this.page,limit:this.limit}
            searchShopList(data).then(res=>{
                // console.log(res);
                this.delivery = res.data.delivery
                if(this.storeList.length){
                    this.storeList = this.storeList.concat(res.data.basic);
                    this.$refs.loadmore.onBottomLoaded();
                }else{
                    this.storeList = res.data.basic
                }
                if(this.storeList.length < this.limit){
                    this.allLoaded = true;
                }
            })
        },
        clearSearch(){
            if(!this.searchText)return;
            this.searchText = "";
            // this.getStoreList();
        },
        getClassList(){
            const data = {
                token:this.$store.getters.token
            }
            let i = 0;
            let class_list = []
            classList(data).then(res=>{
                console.log(res);
                for(let a in res.data){
                    console.log((parseInt(a)+1)%10);
                    if((parseInt(a)+1)%10 == 0){
                        class_list.push(res.data[a]);
                        this.class_list.push(class_list);
                        class_list = [];
                        i++;
                    }else if(a == res.data.length - 1){
                        class_list.push(res.data[a]);
                        this.class_list.push(class_list);
                    }else{
                        class_list.push(res.data[a]);
                    }
                    // console.log(this.class_list);
                }
            })
        }
    },
    components:{
        storeList
    }
}
</script>
<style scoped>
    /* .item {
        height: 5.665rem;
        display: flex;
        align-items: center;
        margin: 0 0.6rem;
    } */
    /* .logo {
        width: 3.5rem;
        height: 3.5rem;
        border-radius: 5px;
        margin-right:1rem;
    }
    .item_text {
        width: 100%;
        display: flex;
        justify-content: space-between;
        line-height:1.5rem;
        font-size: 0.8rem;
    } */
    /* .text-content {
        width: 85%;
    } */
    /* .store_title {
        font-size: 1rem;
        font-weight: bolder;
        max-width: 70%;
        overflow: hidden;
        text-overflow:ellipsis;
        white-space: nowrap;
    } */
    /* .title_icon {
        color: #02a774;
        font-size: 1.5rem;
    }
    .subTitle_icon {
        font-size: 1rem;
        padding-right: 0.25rem;
    } */
    a {
    color: #333;
    }
    .search-content{
        width: 100%;
        height: 3rem;
        display: flex;
        align-items: center;
        justify-content: space-around;
        /* margin-top: 0.2rem; */
        position: relative;
    }
    .search-content input{
        width: 80%;
        height: 2rem;
        border: 2px solid #ccc;
        border-radius: 1.5rem;
        padding: 0 0.5rem;
        font-size: 1rem;
        background: #f5f5f5;
    }
    .search-content input:focus{
        border: 5px solid #F1463C;
        outline: none
    }
    .open::before{
        display: inline-block;
        transform: rotateZ(-180deg);
        -webkit-transform: rotateZ(-180deg);
        -moz-transform: rotateZ(-180deg);
        transition: all 0.5s;
    }
    .hide::before{
        display: inline-block;
        transition: all 0.5s;
    }
    .search-btn{
        background: #F1463C;
        color: #fff;
        height: 2rem;
        font-size: 1rem;
        width: 4rem;
        border-radius: 1.5rem;
    }
    .class-list{
        width: 100%;
        height: 10rem;
        border-bottom: 0.6rem solid #f2f2f2;
        /* margin-top: 0.5rem; */
        /* border-top: 0.1rem solid #ccc; */
    }
    .calss-page{
        display: flex !important;
        justify-content: space-between;
        align-items: center;
        flex-direction: row;
        flex-wrap: wrap;
    }
    .class-item{
        width: 18%;
        margin: 0 2%;
        height: 50%;
        /* border: 0.1rem solid red; */
        /* border-radius: 50%; */
        text-align: center;
    }
    .class-text{
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
    }
    .class-img{
        width: 70%;
        height: 60%;
        border-radius: 50%;
    }
    .select-box{
        position: absolute;
        top: 3rem;
        background: #eee;
        z-index: 1000;
        width: 4rem;
        line-height: 2rem;
        border-radius: 10px;
        text-align: center;
    }
    /* .product-list{
        width: 100%;
        white-space: nowrap;
        overflow: auto;
        border-bottom: 2px solid #ccc;
    } */
    /* .product-list div{
        width: 4rem;
        height: 6rem;
        display: inline-block;
        text-align: center;
        padding: 0.4rem;
    } */
    /* .product-img{
        width: 4rem;
        height: 4rem;
        margin: auto;
    }
    .product-price{
        color: red;
    } */
    /* .product-text{
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
    } */
    .claer-btn{
        background: #fff;
        border: none;
        font-weight: bolder;
        position: absolute;
        right: 5rem;
    }
    .claer-btn .iconfont{
        font-size: 1rem;
    }
    .claer-btn:focus{
        outline: none;
    }
    .class-list >>> .mint-swipe-indicator{
        width: 1rem;
        border-radius: 5px;
        opacity: 0.5;
    }
    .class-list >>> .mint-swipe-indicator.is-active{
        background: #fff;
    }
    .store-list{
        height: Calc(100vh - 22rem);
        overflow: scroll;
    }
    .icon{
        font-size: 1.2rem;
    }
</style>